﻿<%@ WebHandler Language="C#" Class="ZSIssueMissingPartsReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 缺件信息查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.6.17
 **/

public class ZSIssueMissingPartsReport : IHttpHandler
{
    private uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        //查询
        if (jObject["type"] != null && jObject["type"].ToString() == "getData")
        {
            try
            {
                Result r = getData(jObject);
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }

        if (jObject["type"] != null && jObject["type"].ToString() == "getDetail")
        {
            try
            {
                Result r = getDetail(jObject);
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }

        }
    }

    Result getData(JObject jObject)
    {
        Result r = new Result();
        string sql = @"
SELECT DISTINCT U.ATTRIBUTEVALUE,MFG.MFGORDERNAME,EP.CONTAINERNO,WFS.WORKFLOWSTEPNAME,pb.productname,p.description
FROM ENGINEPARTPLAN EP
LEFT JOIN WORKFLOWSTEP WFS ON EP.WORKFLOWSTEPID = WFS.WORKFLOWSTEPID
INNER JOIN (SELECT E.CONTAINERNO, MIN(WFS.SEQUENCE) WFSSEQ
            FROM ENGINEPARTPLAN E
            LEFT JOIN WORKFLOWSTEP WFS ON E.WORKFLOWSTEPID = WFS.WORKFLOWSTEPID
            WHERE E.USECOMPLETED = '0' AND E.CONTAINERID IS NOT NULL
            GROUP BY E.CONTAINERNO
           ) EP1 ON EP.CONTAINERNO = EP1.CONTAINERNO AND WFS.SEQUENCE = EP1.WFSSEQ
LEFT JOIN MFGORDER MFG ON EP.MFGORDERID = MFG.MFGORDERID
LEFT JOIN USERATTRIBUTE U ON MFG.MFGORDERID = U.PARENTID
LEFT JOIN PARTMENUINFO PM ON EP.CONTAINERNO = PM.CONTAINERNO
LEFT JOIN product p ON p.productid = ep.productid --部件件号
LEFT JOIN productbase pb ON pb.productbaseid = p.productbaseid
LEFT JOIN (SELECT IM.PARTMENUINFOID,IM.CONTAINERID,SUM(IM.ISSUEQTY - NVL(ISC.LOSSQTY, 0)) ISSUEQTY
           FROM ISSUEERPMATERIAL IM
           LEFT JOIN (SELECT ISC.ISSUEERPMATERIALID,SUM(ISC.LOSSQTY) LOSSQTY
                      FROM ISSUEERPMATERIALSCRAPINFO ISC
                      GROUP BY ISC.ISSUEERPMATERIALID
                     ) ISC ON IM.ISSUEERPMATERIALID = ISC.ISSUEERPMATERIALID
           GROUP BY IM.PARTMENUINFOID, IM.CONTAINERID
          ) IM ON PM.PARTMENUINFOID = IM.PARTMENUINFOID
WHERE U.USERATTRIBUTENAME = '发动机' AND ep.containerid IS NOT NULL
      AND IM.CONTAINERID IN
                            (SELECT DISTINCT IM.CONTAINERID
                             FROM ISSUEERPMATERIAL IM
                             INNER JOIN (SELECT IM.CONTAINERNO,MAX(IM.CREATEDATE) CREATEDATE
                                         FROM ISSUEERPMATERIAL IM
                                         GROUP BY IM.CONTAINERNO
                                         ) IMN ON IM.CONTAINERNO = IMN.CONTAINERNO AND IM.CREATEDATE = IMN.CREATEDATE
                            )
     AND pm.firstassembleqty-issueqty>0 
";
        if (jObject["containerno"] != null && jObject["containerno"].ToString() != "")
        {
            sql += string.Format(" AND ep.containerno = '{0}'", jObject["containerno"].ToString());
        }
        if (jObject["mfgordername"] != null && jObject["mfgordername"].ToString() != "")
        {
            sql += string.Format(" AND mfg.mfgordername = '{0}'", jObject["mfgordername"].ToString());
        }
        if (jObject["engineno"] != null && jObject["engineno"].ToString() != "")
        {
            sql += string.Format(" AND u.attributevalue = '{0}'", jObject["engineno"].ToString());
        }
        if (jObject["partname"] != null && jObject["partname"].ToString() != "")
        {
            sql += string.Format(" AND pb.productname = '{0}'", jObject["partname"].ToString());
        }

        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);

        DataTable dt = dto.DBTable;

        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    Result getDetail(JObject jObject)
    {
        string sql = @"
SELECT U.ATTRIBUTEVALUE,MFG.MFGORDERNAME,EP.CONTAINERNO,WFS.WORKFLOWSTEPNAME,PB.PRODUCTNAME,P.DESCRIPTION,
       PM.FIRSTASSEMBLEQTY REQUESTQTY,IM.ISSUEQTY,PM.FIRSTASSEMBLEQTY - IM.ISSUEQTY LACKQTY
FROM ENGINEPARTPLAN EP
LEFT JOIN WORKFLOWSTEP WFS ON EP.WORKFLOWSTEPID = WFS.WORKFLOWSTEPID
INNER JOIN (SELECT E.CONTAINERNO, MIN(WFS.SEQUENCE) WFSSEQ
            FROM ENGINEPARTPLAN E
            LEFT JOIN WORKFLOWSTEP WFS ON E.WORKFLOWSTEPID = WFS.WORKFLOWSTEPID
            WHERE E.USECOMPLETED = '0' AND E.CONTAINERID IS NOT NULL
            GROUP BY E.CONTAINERNO
           ) EP1 ON EP.CONTAINERNO = EP1.CONTAINERNO AND WFS.SEQUENCE = EP1.WFSSEQ
LEFT JOIN MFGORDER MFG ON EP.MFGORDERID = MFG.MFGORDERID
LEFT JOIN USERATTRIBUTE U ON MFG.MFGORDERID = U.PARENTID
LEFT JOIN PARTMENUINFO PM ON EP.CONTAINERNO = PM.CONTAINERNO
LEFT JOIN product p ON PM.PRODUCTID = P.PRODUCTID --零件件号
LEFT JOIN productbase pb ON pb.productbaseid = p.productbaseid
LEFT JOIN (SELECT IM.PARTMENUINFOID,IM.CONTAINERID,SUM(IM.ISSUEQTY - NVL(ISC.LOSSQTY, 0)) ISSUEQTY
           FROM ISSUEERPMATERIAL IM
           LEFT JOIN (SELECT ISC.ISSUEERPMATERIALID,SUM(ISC.LOSSQTY) LOSSQTY
                      FROM ISSUEERPMATERIALSCRAPINFO ISC
                      GROUP BY ISC.ISSUEERPMATERIALID
                     ) ISC ON IM.ISSUEERPMATERIALID = ISC.ISSUEERPMATERIALID
           GROUP BY IM.PARTMENUINFOID, IM.CONTAINERID
          ) IM ON PM.PARTMENUINFOID = IM.PARTMENUINFOID
WHERE U.USERATTRIBUTENAME = '发动机' AND ep.containerid IS NOT NULL
      AND IM.CONTAINERID IN
                            (SELECT DISTINCT IM.CONTAINERID
                             FROM ISSUEERPMATERIAL IM
                             INNER JOIN (SELECT IM.CONTAINERNO,MAX(IM.CREATEDATE) CREATEDATE
                                         FROM ISSUEERPMATERIAL IM
                                         GROUP BY IM.CONTAINERNO
                                         ) IMN ON IM.CONTAINERNO = IMN.CONTAINERNO AND IM.CREATEDATE = IMN.CREATEDATE
                            )
     AND pm.firstassembleqty-issueqty>0 
";
        if (jObject["containerno"] != null && jObject["containerno"].ToString() != "")
        {
            sql += string.Format(" AND ep.containerno = '{0}'", jObject["containerno"].ToString());
        }
        if (jObject["mfgordername"] != null && jObject["mfgordername"].ToString() != "")
        {
            sql += string.Format(" AND mfg.mfgordername = '{0}'", jObject["mfgordername"].ToString());
        }
        if (jObject["engineno"] != null && jObject["engineno"].ToString() != "")
        {
            sql += string.Format(" AND u.attributevalue = '{0}'", jObject["engineno"].ToString());
        }
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);

        DataTable dt = dto.DBTable;

        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }
}